<div class="panel bg2">
	<div class="inner"><span class="corners-top"><span></span></span>

	<!-- IF S_SHOW_ATTACH_BOX -->
	<h3>{L_ADD_ATTACHMENT}</h3>
	<fieldset class="fields2">
	<dl>
		<dt><label for="fileupload">{L_FILENAME}:</label></dt>
		<dd>
			<input type="file" name="fileupload" id="fileupload" maxlength="{FILESIZE}" value="" class="inputbox autowidth" /> 
			<input type="submit" name="add_file" value="{L_ADD_FILE}" class="button2" onclick="upload = true;" />
			<input type="button" name="clear_file" value="{L_CLEAR}" class="button2 if_file_is_selected" />
		</dd>
	</dl>
	<dl class="if_file_is_selected">
		<dt><label for="filecomment">{L_FILE_COMMENT}:</label></dt>
		<dd><textarea name="filecomment" id="filecomment" rows="1" cols="40" class="inputbox">{FILE_COMMENT}</textarea></dd>
	</dl>
	</fieldset>
	<!-- ENDIF -->

	<!-- IF S_HAS_ATTACHMENTS -->
	<h3>{L_POSTED_ATTACHMENTS}</h3>
	<fieldset class="fields2">
		<!-- BEGIN attach_row -->
		<dl>
			<dt><label for="comment_list_{attach_row.ASSOC_INDEX}">{L_FILE_COMMENT}:</label></dt>
			<dd><textarea name="comment_list[{attach_row.ASSOC_INDEX}]" id="comment_list_{attach_row.ASSOC_INDEX}" rows="1" cols="40" class="inputbox">{attach_row.FILE_COMMENT}</textarea></dd>
			<dd><a href="{attach_row.U_VIEW_ATTACHMENT}">{attach_row.FILENAME}</a></dd>
			<dd style="margin-top: 5px;">
				<!-- IF S_INLINE_ATTACHMENT_OPTIONS --><input type="button" value="{L_PLACE_INLINE}" onclick="attach_inline({attach_row.ASSOC_INDEX}, '{attach_row.A_FILENAME}');" class="button2" />&nbsp; <!-- ENDIF -->
				<input type="submit" name="delete_file[{attach_row.ASSOC_INDEX}]" value="{L_DELETE_FILE}" class="button2" />&nbsp; 
				<input type="submit" name="update_file[{attach_row.ASSOC_INDEX}]" value="{L_UPDATE_FILE}" class="button2 if_file_is_selected" />
			</dd>
		</dl>
		{attach_row.S_HIDDEN}
			<!-- IF not attach_row.S_LAST_ROW --><hr class="dashed" /><!-- ENDIF -->
		<!-- END attach_row -->
	</fieldset>
	<!-- ENDIF -->

	<span class="corners-bottom"><span></span></span></div>
</div>
<script>
jQuery(function($)
{
	var check_file = function(input, filename)
	{
		<!-- IF ALLOWED_EXTENSIONS_JSON -->
		var ext = null;
		if (!(ext = filename.match(/\.[\w\d]+$/)))
		{
			alert('{L_ATTACH_UNALLOWED_EXT}');
			return false;
		}
		var allowed = {ALLOWED_EXTENSIONS_JSON};
		ext = ext[0].substring(1).toLowerCase();
		if (!allowed[ext])
		{
			alert('{L_ATTACH_UNALLOWED_EXT}');
			return false;
		}

		// We can't get file size
		if (!input || !window.FileReader || !input.files || !input.files[0] || !input.files[0].size)
		{
			return true;
		}

		if (input.files[0].size > allowed[ext])
		{
			var size_text = '';
			if (allowed[ext] < 1024)
				size_text = allowed[ext].toString() + ' {L_BYTES}';
			else if (allowed[ext] / 1024 < 1024)
				size_text = (allowed[ext]/1024.0).toFixed(2) + ' {L_KIB}';
			else
				size_text = (allowed[ext]/1048576.0).toFixed(2) + ' {L_MIB}';
			alert('{L_ATTACH_IS_TOO_BIG}' + size_text + '.');
			return false;
		}
		<!-- ENDIF -->
		return true;
	}

	$('#postform input[name=fileupload]').on('change', function()
	{
		var filename = $(this).val();

		if (filename != '' && !check_file(this, filename))
		{
			$(this).wrap('<form>').closest('form').get(0).reset();
			$(this).unwrap();
			filename = '';
		}

		if (filename == '')
		{
			$('.if_file_is_selected').hide();
			$('#postform input[name=add_file]').prop('disabled', true);
		}
		else
		{
			$('.if_file_is_selected').show();
			$('#postform input[name=add_file]').prop('disabled', false);
		}
	});
	$("#postform input[name=fileupload]").change();

	$("#postform input[name=clear_file]").on('click', function()
	{
		var $input = $('#postform input[name=fileupload]');
		$input.wrap('<form>').closest('form').get(0).reset();
		$input.unwrap();
		$input.change();
	});
});
</script>
